﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace GDM.Hogar.Controles.ControlesPersonalizados
{
    public partial class StyledDecimalTextBox : TextBox
    {
        public StyledDecimalTextBox()
        {
            TextAlign = HorizontalAlignment.Right;
            InitializeComponent();
        }

        public override bool AutoSize
        {
            get
            {
                return true;
            }
            set
            {
                base.AutoSize = value;
            }
        }

        public override Font Font
        {
            get
            {
                return new Font("Lucida Sans", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
            }
            set
            {
                base.Font = value;
            }
        }

        public override Color ForeColor
        {
            get
            {
                return Color.Black;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        [DefaultValue(typeof(HorizontalAlignment), "Right"),
        Category("Appearance")]
        public new HorizontalAlignment TextAlign
        {
            get { return base.TextAlign; }
            set { base.TextAlign = value; }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            var ch = e.KeyChar;
            
            if (ch == 44 && Text.IndexOf(',') != -1)
            {
                e.Handled = true;
                return;
            }

            if (!Char.IsDigit(ch) && ch != 8 && ch != 44)
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }
    }
}